package com.tyjc.unique;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.tyjc.unique.view.PrizeSsqView;
import com.tyjc.lottery.R;

import org.xutils.view.annotation.ContentView;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * <h1><b>功能说明</b></h1>
 * <p>从33个红球和16个篮球中机选出5注号码各不相同的注码</p></br>
 * <p>机选周期为天</p></br>
 * @author miao
 *
 */
@ContentView(R.layout.fg_ssqluckynum)
public class LuckyNumFragment extends AnaylaseBaseFragment {

	private ListView luckynums_view;
	private Button refresh;
	private TextView luck_txt;
	private ArrayList<int[]> luckynums_data;

	private LuckNumAdapter adapter;


	@Override
	public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		refresh = (Button)view.findViewById(R.id.update_luckynum);
		luckynums_view =  view.findViewById(R.id.lucky_nums);
		luck_txt = (TextView)view.findViewById(R.id.lucky_txt);
		luck_txt.setText("");
		refresh.setOnClickListener(this);
		initLuckyNums();
	}

	private void initLuckyNums(){
		luckynums_data = new ArrayList<>();
		randomNoRepeat( 33);
		adapter = new LuckNumAdapter(getContext());
		luckynums_view.setAdapter(adapter);
		
	}
	
	
	@Override
	public void onClick(View view) {
		super.onClick(view);
		switch (view.getId()) {
		case R.id.update_luckynum:
			randomNoRepeat(33);
			adapter.notifyDataSetChanged();
			break;

		default:
			break;
		}
	}

	public   void randomNoRepeat(int max){
		luckynums_data.clear();
		ArrayList<Integer> allNumArr = new ArrayList<Integer>();
		ArrayList<Integer> blueArr = new ArrayList<Integer>();
		for(int i = 1 ; i < 34 ; i ++){
			allNumArr.add(i);
		}
		for(int x = 0 ; x < 5 ; x++){
			int[] arr = new int[6];
			ArrayList<Integer> randomArr = new ArrayList<Integer>();
			for (int i = 0; i < 6; i++) {
				int j = (int) (Math.random() * allNumArr.size()) ;//机选出6个为主信息
				while (randomArr.contains(allNumArr.get(j))) {
					j = (int) (Math.random() * allNumArr.size()) ;
				}
				randomArr.add(allNumArr.get(j));
				arr[i] = allNumArr.get(j);
				allNumArr.remove(j);
			}
			Arrays.sort(arr);
			int[] arrs = new int[7];
			for(int z = 0 ; z < 7 ; z ++){
				if(z<6){
					arrs[z] = arr[z];
				}else{
					arrs[z] = (int) (Math.random() * 16) + 1;
					while(blueArr.contains((Integer)arrs[z])){
						arrs[z] = (int) (Math.random() * 16) + 1;
					}
					blueArr.add(arrs[z]);
				}
			}
			luckynums_data.add(arrs);
		}
	}
	
	
	public class LuckNumAdapter extends BaseAdapter {
		
		private Context context;
		
		public LuckNumAdapter(Context context){
			this.context = context;
		}

		@Override
		public int getCount() {
			return luckynums_data.size();
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			PrizeSsqView ssqView ;
			if(convertView == null){
				ssqView = new PrizeSsqView(context);
				convertView = ssqView;
			}else{
				ssqView = (PrizeSsqView) convertView;
			}
			ssqView.setNum(luckynums_data.get(position));
			return convertView;
		}
		
	}
	
}
